package com.chunuo.gulimall.product.service.impl;

import com.chunuo.gulimall.product.entity.AttrEntity;
import com.chunuo.gulimall.product.service.AttrService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chunuo.common.utils.PageUtils;
import com.chunuo.common.utils.Query;

import com.chunuo.gulimall.product.dao.ProductAttrValueDao;
import com.chunuo.gulimall.product.entity.ProductAttrValueEntity;
import com.chunuo.gulimall.product.service.ProductAttrValueService;
import org.springframework.transaction.annotation.Transactional;


@Service("productAttrValueService")
public class ProductAttrValueServiceImpl extends ServiceImpl<ProductAttrValueDao, ProductAttrValueEntity> implements ProductAttrValueService {

    @Autowired
    private AttrService attrService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<ProductAttrValueEntity> page = this.page(
                new Query<ProductAttrValueEntity>().getPage(params),
                new QueryWrapper<ProductAttrValueEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<ProductAttrValueEntity> baseAttrlistforspu(Long spuId) {
        return null;
    }

    @Override
    public void saveProductAttr(List<ProductAttrValueEntity> collect) {
        this.saveBatch(collect);
    }

    @Override
    public List<ProductAttrValueEntity> queryListBySpuId(Long spuId) {
        List<ProductAttrValueEntity> productAttrValueEntityList =
                this.list(new QueryWrapper<ProductAttrValueEntity>().eq("spu_id", spuId));
        for(ProductAttrValueEntity entity : productAttrValueEntityList){
            AttrEntity byId = attrService.getById(entity.getAttrId());
            entity.setValueType(byId.getValueType());
        }

        return productAttrValueEntityList;
    }

    /**
     * 更新规格
     * @param spuId
     * @param entitys
     */
    @Transactional
    @Override
    public void updateListForspu(Long spuId, List<ProductAttrValueEntity> entitys) {
        this.remove(new QueryWrapper<ProductAttrValueEntity>().eq("spu_id",spuId));
        for(ProductAttrValueEntity entity : entitys){
            entity.setSpuId(spuId);
        }
        this.saveBatch(entitys);
    }

}